package com.springboot.springboot3.Mybatis.controller;

import com.springboot.springboot3.Mybatis.service.AaService;
import com.springboot.springboot3.Mybatis.service.CityService;
import com.springboot.springboot3.Mybatis.service.UserService;
import com.springboot.springboot3.bean.Aa;
import com.springboot.springboot3.bean.City;
import com.springboot.springboot3.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @ClassName:
 * @author: CKC
 * @DateTime: 2021/1/29 10:06
 */
@Controller
public class IndexController {
    @Autowired
    AaService aaService;
    
    @ResponseBody
    @GetMapping(value = "/Aa")
    public Aa getById(@RequestParam("id") Integer id) {
        
        return aaService.getAaById(id);
    }
    
    @Autowired
    CityService cityService;
    
    @ResponseBody
    @PostMapping("/city")
    public City saveCity(City city) {
        cityService.save(city);
        return city;
    }
    
    @ResponseBody
    @GetMapping(value = "/city")
    public City getCityById(@RequestParam("id") Long id) {
        return cityService.getById(id);
    }
    
    @GetMapping("city.html")
    public String city(RedirectAttributes attributes) {
//        attributes.addFlashAttribute("id", "1");
        
        return "city";
    }
    
    @Autowired
    UserService userService;
    
    @ResponseBody
    @GetMapping("/user")
    public List<User> userList() {
        //从数据库中查出user表中的用户
        List<User> users = userService.list();
        return users;
    }
    
    
    
    /**JdbcTemplate 测试*/
    @Autowired
    JdbcTemplate jdbcTemplate;
    
    @ResponseBody
    @GetMapping(value = "/sql")
    public String sds() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from aa where 1=?", 1);
        return Arrays.asList(list).toString();
    }
    
}
